package com.tgy.algorithm._经典题目01;

public class _001_绳子覆盖最多轴上数 {

    public static int axiosNums(int[] nums, int k) {

        if (nums == null || nums.length == 0) {
            return 0;
        }

        int len = nums.length;
        int leftIndex = 0;
        int rightIndex = 0;
        int maxLen = 0;
        while (rightIndex < len) {
            while (rightIndex < len && nums[rightIndex] - nums[leftIndex] <= k) {
                rightIndex++;
            }
            maxLen = Math.max(maxLen,rightIndex - leftIndex);
            leftIndex++;
        }

        return maxLen;
    }

    /**
     * 返回的是位置
     * @param nums
     * @param k 当没有值的时候，返回-1
     * @return
     */
    public static int getGE(int[] nums, int k) {

        if (nums == null || nums.length == 0) {
            return -1;
        }

        int left = 0;
        int right = nums.length - 1;
        int center;
        int retIndex = -1;
        while (left <= right) {
            center = left + ((right - left) >> 1);
            if (nums[center] >= k) {
                retIndex = center;
                right = center - 1;
            }else {
                left = center + 1;
            }
        }

        return retIndex;
    }

    public static void main(String[] args) {
        int[] nums = {2,3,5,5,7,9};
//        int maxLen = axiosNums(nums, 4);
//        System.out.println(maxLen);
        int ge = getGE(nums, 10);
        System.out.println(ge);
    }
}
